package com.zy.modules.mall.service.impl;

import com.zy.modules.mall.dao.ProductRepository;
import com.zy.modules.mall.entity.ProductEntity;
import com.zy.modules.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("productService")
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductRepository productRepository;

    @Override
    public ProductEntity queryObject(Long id) {
        return productRepository.findOne(id);
    }

    @Override
    public List<ProductEntity> queryList(Map<String, Object> map) {
        int limit = (int) map.getOrDefault("limit", 10);
        int offset = (int) map.getOrDefault("offset", 0);
        Pageable pageable = new PageRequest(offset, limit);
        Page<ProductEntity> entities = productRepository.findAll(pageable);
        return entities.getContent();
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        long count = productRepository.count();
        return (int) count;
    }

    @Override
    public void save(ProductEntity product) {
        productRepository.save(product);
    }

    @Override
    public void update(ProductEntity product) {
        productRepository.save(product);
    }

    @Override
    public void delete(Long id) {
        productRepository.delete(id);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            productRepository.delete(id);
        }
    }

}
